<template>
  <div class="addstaff">
    <p class="addstaff-title">编辑员工</p>
    <p class="addstaff-tip"><i class="el-icon-warning" style="color: #B9C0C7" />设置完员工后管理员无权修改员工手机号</p>
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="180px" class="demo-ruleForm">
      <el-form-item label="员工手机号" prop="mobile" class="required-not">
        <el-input v-model="ruleForm.mobile" class="w240" placeholder="请输入员工手机号" disabled />
        <i class="el-icon-warning input-tip" style="color: #B9C0C7" />
        <span class="span-tip">员工手机号一旦设置无法修改，员工可自行登录后台修改手机号</span>
        <p style="color: #cbcfd5">保存完毕后首次登录请告知员工使用手机号＋验证码方式登录，登录后可自行设置员工账号登录密码</p>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email" class="w240" placeholder="请输入邮箱" :disabled="currentRoleId != -1" />
        <i class="el-icon-warning input-tip" style="color: #B9C0C7" />
        <span class="span-tip">仅超级管理员可修改员工邮箱，员工可自行登录后台修改邮箱</span>
      </el-form-item>
      <el-form-item label="员工备注名" prop="nickName">
        <el-input v-model="ruleForm.nickName" class="w240" placeholder="请输入员工备注名" />
      </el-form-item>
      <el-form-item label="角色" prop="roleId">
        <el-radio-group v-model="ruleForm.roleId">
          <el-radio v-for="item in roleList" :key="item.roleId" class="mb16" :label="item.roleId" :disabled="item.roleId == -1">{{ item.roleName }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-button style="margin-left: 10px" @click="cancelBtn">返回</el-button>
      <el-button type="primary" style="margin-left: 10px" @click="submitForm('ruleForm')">确定</el-button>
    </el-form>
  </div>
</template>
<script>
import { validateEmail } from '@/utils/validate'
import api from '@/api/index'
export default {
  name: 'StaffInfo',
  components: {},
  props: ['editDetail'],
  data() {
    return {
      currentRoleId: 0,
      roleList: [],
      ruleForm: {
        mobile: '',
        email: '',
        nickName: '',
        roleId: '',
        pathurl:this.$store.getters.imShowId.substring(0,2).toLowerCase()=="tw"?'twBoss':'PtBoss',
      },
      rules: {
        mobile: [
        ],
        email: [
          { required: true, validator: validateEmail, trigger: 'blur' }
        ],
        nickName: [
          { required: true, message: '请输入员工备注名', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    const data = JSON.parse(JSON.stringify(this.editDetail))
    this.ruleForm = {
      id: data.id,
      mobile: data.mobile,
      email: data.email,
      nickName: data.nickname,
      roleId: data.roleId,
      pathurl:this.$store.getters.imShowId.substring(0,2).toLowerCase()=="tw"?'twBoss':'PtBoss'
    }
    this.getQueryAccountRole()
    this.getCurrentRole()
  },
  methods: {
    async getCurrentRole() {
      const searchParams ={
        imShowId:this.$store.getters.imShowId,
        imListId:this.$store.getters.imListId,
        pathurl:this.$store.getters.imShowId.substring(0,2).toLowerCase()=="tw"?'twBoss':'boss',
        userName:this.$store.getters.username,
        userId:this.$store.getters.useid,
        mobile:this.$store.getters.mobile
      }
            if (this.$store.getters.imShowId.substring(0,2).toLowerCase()=="tw") {
        let form={
          imListId:this.$store.getters.imListId,
          limit:10,
          page:1,
          text:this.$store.getters.mobile
        }
        const data = await api.specialist.queryAccountList(form)
        this.currentRoleId = data.data[0].roleId
      }else{
        const data = await api.boss.getCompanyUserDetail(searchParams)
        this.currentRoleId = data.roleId
      }
      

    },
    getChangeTem() {
      const temState = {
        isStaffIndex: false,
        isAddStaff: true
      }
      this.$emit('templateChange', temState)
    },
    cancelBtn() {
      const temState = {
        isStaffIndex: true,
        isAddStaff: false,
        isAddAccount: false,
        isEditStaff: false
      }
      this.$emit('templateChange', temState)
    },
    submitForm(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          const { data } = await api.boss.updateAccountInfo(this.ruleForm)
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this.cancelBtn()
        } else {
          return false
        }
      })
    },
    // 获取权限列表
    async getQueryAccountRole() {
      const searchParams ={
        imListId:this.$store.getters.imListId,
        pathurl:this.$store.getters.imShowId.substring(0,2).toLowerCase()=="tw"?'twBoss':'boss'
      }
      const { data } = await api.boss.queryAccountRoleList(searchParams)
      this.roleList = data
    }
  }
}
</script>
<style lang="scss" scoped>
  .addstaff {
    width: 100%;
    height: 100%;
    padding: 16px 16px 16px 32px;
    box-sizing: border-box;
    position: relative;
    overflow-y: auto;
    .addstaff-title {
      font-size: 16px;
      font-weight: bold;
      line-height: 1.5;
      color: #202933;
      padding-left: 8px;
      position: relative;
      &::before {
        content: "";
        width: 2px;
        height: 14px;
        background: #0C7FF2;
        position: absolute;
        left: 0;
        top: 50%;
        margin-top: -7px;
      }
    }
    .addstaff-tip {
      font-size: 12px;
      color: #7E8790;
      line-height: 1.5;
      margin: 4px auto 20px;
      i {
        margin-left: 5px;
      }
    }
    ::v-deep .el-form {
      .el-form-item {
        .el-form-item__label {
          text-align: left;
        }
        &.required-not {
          margin-left: 10px;
          .el-form-item__label {
            width: 170px!important;
            text-align: left;
            padding-right: 0;
          }
          .el-form-item__content {
            margin-left: 170px!important;
          }
        }
        .el-form-item__content {
          .input-tip {
            color: #E53917;
            font-size: 14px;
            margin: 0 10px 0 15px;
            display: inline-block;
          }
          .span-tip {
            font-size: 14px;
            color: #7E8790;
          }
        }
      }
    }
  }
</style>
